package com.example.lottery_system.controller;

import com.example.lottery_system.common.errcode.ControllerErrorCodeConstant;
import com.example.lottery_system.common.exception.ControllerException;
import com.example.lottery_system.common.utils.JacksonUtil;
import com.example.lottery_system.common.utils.RegexUtil;
import com.example.lottery_system.controller.param.ShortMessageParam;
import com.example.lottery_system.controller.param.UserPasswordParam;
import com.example.lottery_system.controller.result.UserListResult;
import com.example.lottery_system.service.UserService;
import com.example.lottery_system.service.VerificationCodeService;
import com.example.lottery_system.service.dto.UserListDto;
import com.example.lottery_system.service.dto.UserRegisterDto;
import com.example.lottery_system.controller.param.UserRegisterParam;
import com.example.lottery_system.controller.result.UserLoginResult;
import com.example.lottery_system.controller.result.UserRegisterResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class UserController {
    /**
     * 注册接口
     */

    @Autowired
    VerificationCodeService verificationCodeService;
    @Autowired
    UserService userService;

    @RequestMapping("/register")
    public UserRegisterResult userRegister(@RequestBody @Validated UserRegisterParam param) {
        log.info("用户注册传入的参数为：" + JacksonUtil.writeValueAsString(param));

       UserRegisterDto userRegisterDto = userService.register(param);

       UserRegisterResult userRegisterResult = new UserRegisterResult();
       userRegisterResult.setId(userRegisterDto.getId());
       return userRegisterResult;
    }

    @RequestMapping("/login/password")
    public UserLoginResult userLogin(@RequestBody @Validated UserPasswordParam param) {
        log.info("用户登录传入的参数为： " + JacksonUtil.writeValueAsString(param));
        return  userService.login(param);
    }

    @RequestMapping("/login/shortMsg")
    public UserLoginResult userLogin(@RequestBody @Validated ShortMessageParam param) {
        log.info("用户登录传入的参数为： " + JacksonUtil.writeValueAsString(param));
        return  userService.login(param);
    }



    @RequestMapping("/login/sendCode")
    public boolean sendVerificationCode(String number) {
        log.info("发送验证码传入的手机号/邮箱为:{} " , number);
        try {
            /**
             * 这里就需要校验传入的手机号还是邮箱
             */
            if (RegexUtil.checkMail(number)) {
                verificationCodeService.sendVerificationCodeByEmail(number);
            } else if (RegexUtil.checkMobile(number)) {
                verificationCodeService.sendVerificationCodeByPhone(number);
            } else {
                throw  new Exception();
            }
        } catch (Exception e) {
            throw  new ControllerException(ControllerErrorCodeConstant.LOGIN_CODE_ERROR);
        }

        return true;
    }


    @RequestMapping("/getUserList")
    public List<UserListResult> getUserList(String identity) {
       log.info("获取用户列表的身份信息：{} ", identity);

       List<UserListDto> list =  userService.getUserDtoList(identity);
       List<UserListResult> userListResults =
               list.stream().map(userListDto->
                       new UserListResult(userListDto.getId(),userListDto.getUserName(),userListDto.getIdentity().getMsg()
               )).collect(Collectors.toList());
       // 对象的转化

        return userListResults;

    }

    @RequestMapping("/deleteUser")
    public Boolean deleteUser(Long userId) {
        log.info("deleteUser::userId:{}",userId);
        return userService.deleteUser(userId);
    }

}

